package cn.itcast.bos.web.action;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;






import java.util.HashMap;
import java.util.Map;

import org.springframework.data.domain.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{

	  //模型驱动
	protected T model;

	@Override
	public T getModel() {
		
		return model;
	}
	//构造器完成model实例化
	public BaseAction(){
		Type genericSuperclass = this.getClass().getGenericSuperclass();
		//获取类型第一个泛型参数
		ParameterizedType parameterizedType =(ParameterizedType) genericSuperclass;
	Class<T> modelClass = (Class<T>) parameterizedType.getActualTypeArguments()[0];
		//实例化
	try {
		model = modelClass.newInstance();
	} catch (InstantiationException e) {
		
		e.printStackTrace();
	} catch (IllegalAccessException e) {
	
		e.printStackTrace();
		System.out.println("模型构造失败");
	}
	}
	//接受分页查询数据
	protected int page;
	protected int rows;

	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	//将分页查询结果数据，压入值栈
	protected void push(Page<T> pageDate) {
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("rows", pageDate.getContent());
		map.put("total", pageDate.getTotalElements());
		ActionContext.getContext().getValueStack().push(map);
	}
}
